#!/usr/bin/perl
# Version 0.2.1

$testcase = "seq_num_wrap";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 1) {
        print("Usage: $testcase.pl dump_file log_file");
        exit 1;
}

$dumpfile = $ARGV[0];
$logfile = $ARGV[1];

#------------------------------------------------------------------------------

$state = 0;

# Open the dumpfile
$success = 1;
open(DUMPFILE, $dumpfile) or $success = 0;

if ($success eq 1) {
		
	while(<DUMPFILE>) {

		if ($state eq 0) {		
			# first look for an update from 0.0.0.2 for 0.0.0.1's router LSA having
			# sequence number 0x7fffffff
			if(/(.*) 0\.0\.0\.2 > (.*): OSPFv2-ls_upd (.*) S 7fffffff (.*) rtr 0\.0\.0\.1 (.*)/) {
				# here we go... from now on, there are important packets!
				$state = 1;
			} 
		} else {		
			# only look at upd packets containing router 0.0.0.1's router LSA, 
			# originated by router 0.0.0.1
			if(/(.*) 0\.0\.0\.1 > (.*): OSPFv2-ls_upd (.*) rtr 0\.0\.0\.1 (.*)/) {
				if($state eq 1) {
					# in state 1 the RUT must max age the lsa with seq. no. 7fffffff
					if($3 =~ /S 7fffffff age 3600/) {
						# GREAT! The RUT did its job!
						$state = 2;
					}
				} elsif ($state eq 2) {
					# in state 2, the RUT must originate a new LSA with seq. 0x80000001
					if($3 =~ /S 80000001/) {
						# GREAT! The RUT did its job!
						$state = 3;
					}
				}
			}
		}
		
	}
	close(DUMPFILE);
}

if($state eq 0) {
	$verdict = 3;
	print("Test not performed properly. Cannot detemine results. Test INCONCLSIVE.\n");
} elsif($state eq 1) {
	$verdict = 2;
	print("RUT did not max-age the LSA. Test FAILED.\n");
} elsif($state eq 2) {
	$verdict = 2;
	print("After max-aging, the RUT didn' originate a new LSA. Test FAILED.\n");
} else {
	$verdict = 1;
	print("RUT properly max-aged and flooded its LSA. Test PASSED.\n");
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");